package excel;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Author: 郑钰君
 * Date: 2022/4/19 12:08
 * FileName: CommonUtils
 * Description:
 */

@Slf4j
public class CommonUtils {
    static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static <T> void handleFields(T t, Field field, Object value) throws Exception {
        Class<?> type = field.getType();
        if (type == null || type == void.class || value==null) {
            return;
        }
        if (type == Object.class) {
            field.set(t, value);
        } else if (type == int.class || type == Integer.class) {
            field.set(t, NumberUtils.toInt((String) value));
        } else if (type == long.class || type == Long.class) {
            field.set(t, NumberUtils.toLong((String)value));
        } else if (type == byte.class || type == Byte.class) {
            field.set(t, NumberUtils.toByte((String)value));
        } else if (type == short.class || type == Short.class) {
            field.set(t, NumberUtils.toShort((String)value));
        } else if (type == double.class || type == Double.class) {
            field.set(t, NumberUtils.toDouble((String)value));
        } else if (type == float.class || type == Float.class) {
            field.set(t, NumberUtils.toFloat((String)value));
        } else if (type == char.class || type == Character.class) {
            field.set(t, CharUtils.toChar((String)value));
        } else if (type == boolean.class) {
            field.set(t, BooleanUtils.toBoolean((String)value));
        } else if (type == BigDecimal.class) {
            field.set(t, new BigDecimal((String)value));
        } else if (type == Date.class) {
            field.set(t, new Date((String)value));
        } else if (type == Boolean.class) {
            field.set(t, BooleanUtils.toBoolean((String)value));
        } else if (type == String.class) {
            field.set(t, value);
        }else if (type == LocalDate.class){
            field.set(t, LocalDate.parse(String.valueOf(value), dateTimeFormatter));
        } else {
            Constructor<?> constructor = type.getConstructor(String.class);
            field.set(t, constructor.newInstance(value));
        }
    }

    public static String getCellValues(Cell cell) {
        if (cell == null) {
            return "";
        }
        //数值类型,整数,小数,日期
        if (cell.getCellType() == CellType.NUMERIC) {
            //日期与小数和整型类型转换
            if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
                //将Date转换为localDate
                //LocalDate localDate = LocalDateTime.ofInstant(cell.getDateCellValue().toInstant(), ZoneId.systemDefault()).toLocalDate();
                //return dateTimeFormatter.format(localDate);
                return DateUtil.getJavaDate(cell.getNumericCellValue()).toString();
            } else {
                if (cell.getNumericCellValue() % 1 == 0) {
                    return new BigDecimal(cell.getNumericCellValue()).toString();
                }
                return String.valueOf(cell.getNumericCellValue());
            }

        } else if (cell.getCellType() == CellType.STRING) {
            return StringUtils.trimToEmpty(cell.getStringCellValue());
        } else if (cell.getCellType() == CellType.FORMULA) {
            return StringUtils.trimToEmpty(cell.getCellFormula());
        } else if (cell.getCellType() == CellType.BLANK) {
            return "";
        } else if (cell.getCellType() == CellType.BOOLEAN) {
            return String.valueOf(cell.getBooleanCellValue());
        } else if (cell.getCellType() == CellType.ERROR) {
            return "ERROR";
        } else {
            return cell.toString().trim();
        }

    }

}
